package com.woniu.woniuproject.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.woniuproject.entity.Account_log;
import com.woniu.woniuproject.entity.Debt_info;
import com.woniu.woniuproject.entity.LooseLabel;
import com.woniu.woniuproject.mapper.Account_logMapper;
import com.woniu.woniuproject.mapper.Debt_infoMapper;
import com.woniu.woniuproject.mapper.LooseLabelMapper;
import com.woniu.woniuproject.utils.moneyUtil;
import com.woniu.woniuproject.vo.Page;
import com.woniu.woniuproject.vo.queryByok;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 功能描述:<br>
 * 〈〉
 *
 * @author 徐威
 * @create 2020/11/16
 * @since 1.0.0
 */
@Service
public class DebtInfoService {
    @Autowired
    Debt_infoMapper debt_infoMapper;

    public PageInfo<Debt_info> queryByAllbystatus(String debtTypeName, String matchedStatus, Page page){
        PageHelper.startPage(page.getNowPage(),page.getMaxPage());
        List<Debt_info> debt_infos = debt_infoMapper.selectBymatchedStatusAnddebtTypeName(matchedStatus, debtTypeName);
    PageInfo<Debt_info> info = new PageInfo<>(debt_infos);
    return  info;
    }
    @Autowired
    LooseLabelMapper looseLabelMapper;
    @Autowired
    moneyUtil moneyUtils;
    @Autowired
    Account_logMapper account_logMapper;
    @Transactional
    public int  startMatching(int id){
    Debt_info debt_info = debt_infoMapper.selectByPrimaryKey(id);
    String debttypename = debt_info.getDebttypename();
    LooseLabel looseLabel = looseLabelMapper.selectByProjectnumber(debttypename);
    double v = moneyUtils.changeMoney(debt_info.getDabtrate() * 360, debt_info.getAvailablemoney(), Integer.parseInt(debt_info.getLoanperiod()),id,debt_info.getDebttype());
        System.out.println(v+"vvvvvvvvvvvvv");
    debt_info.setMatchedstatus("11401");

        looseLabel.setStatus(3);
        if (v>0){
        if (v==debt_info.getAvailablemoney()){
                    looseLabel.setStatus(5);
                    debt_info.setDebtstatus("产息中");
                }else{
                    debt_info.setDebtstatus("未开始");
                }
                Account_log account_log=account_logMapper.selecrBypid(id);

                debt_info.setContractNos(account_log.getAtransferserialno());
                debt_info.setMatchedstatus("11402");
                looseLabel.setTransferredamount(v);
                debt_info.setCreditor("集团");
                debt_info.setDebtmoney(v);
                debt_info.setMatchedmoney(v);
                debt_info.setBorrowerid(0);
                debt_info.setDebtstatus("0");
                debt_info.setDebttransferreddate(new Date());
                debt_infoMapper.updateStatusByKey(debt_info);
            }else {

        int insert = debt_infoMapper.updateByPrimaryKey(debt_info);
    }
        int rew = looseLabelMapper.updatestatusBykey(looseLabel);
        return  rew ;
    }

    public PageInfo<Debt_info> queryByok(queryByok querybyok) {
        PageHelper.startPage(querybyok.getNowPage(),querybyok.getMaxPage());
       List<Debt_info> lists = debt_infoMapper.selectByOK(querybyok);
       return new PageInfo<>(lists);
    }
    @Transactional
    public int cancelMatching(int id) {
    int status1 = 2;
    String status2 = "11401";
    debt_infoMapper.updateStatusBkey(id,status2);
    Debt_info debt_info = debt_infoMapper.selectByPrimaryKey(id);
    int rew =looseLabelMapper.updateStatusBkey(debt_info.getDebttypename(),status1);
    return rew;
    }
}
